
package com.ukao.pad.presenter;

import android.content.Context;


import com.ukao.pad.base.BasePresenter;
import com.ukao.pad.bean.BalanceBean;
import com.ukao.pad.bean.Coupons;
import com.ukao.pad.bean.StringBean;
import com.ukao.pad.consts.Constant;
import com.ukao.pad.consts.SaveParamets;
import com.ukao.pad.eventbus.PayEvent;
import com.ukao.pad.retrofit.ApiCallback;
import com.ukao.pad.utils.CheckUtils;
import com.ukao.pad.utils.L;
import com.ukao.pad.utils.T;
import com.ukao.pad.utils.Utils;
import com.ukao.pad.view.GatheringView;

import java.util.HashMap;

/**
 * Created by ukaodev on 2017/6/28.
 */

public class GatheringPesenter extends BasePresenter<GatheringView> {

    public GatheringPesenter(GatheringView view) {
        attachView(view);
    }



    /*用户余额*/
    public void userExtbalance(String userId   ) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("id", userId);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.userExtbalance(Constant.createParameter(mHashMap)),
                new ApiCallback<BalanceBean>() {
                    @Override
                    public void onSuccess(BalanceBean model) {
                        if (model.getHttpCode() == 200) {

                            mvpView.useBalanceData(model.getData());

                        } else {

                            mvpView.loadFail(model.getMsg());
                        }

                    }

                    @Override
                    public void onNoNewWork() {

                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }

    /**
     * 使查询订单优惠券
     *
     * @param orderId
     */
    public void couponList(String orderId) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("orderId", orderId);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.couponList(Constant.createParameter(mHashMap)),
                new ApiCallback<Coupons>() {
                    @Override
                    public void onSuccess(Coupons model) {
                        if (model.getHttpCode() == 200) {

                            mvpView.loadCouponList(model.getData());

                        } else {

                            mvpView.loadFail(model.getMsg());
                        }

                    }

                    @Override
                    public void onNoNewWork() {

                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }

    /**
     * 使用优惠券
     *
     * @param orderId
     */
    public void userCouponInfo(String orderId,final Coupons  mCoupons) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("orderId", orderId);
        mHashMap.put("relId", mCoupons.getId());/*订单优惠券id*/
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.userCouponInfo(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode() == 200) {
                             mvpView.userCouponSucceed(mCoupons);
                        } else {
                              mvpView.loadFail(model.getMsg());
                        }

                    }

                    @Override
                    public void onNoNewWork() {

                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }


    /**
     * 余额支付
     * @param orderId
     * @param cpnRelId
     */
    public void balancePay(String orderId,String  cpnRelId ) {
        mvpView.showLoading();

        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("orderId",orderId);
        if (!CheckUtils.isEmpty(cpnRelId)){
            mHashMap.put("cpnRelId", cpnRelId);
        }
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        L.i("balancePay="+mHashMap.toString());
        addSubscription(apiStores.balancePay(Constant.createParameter(mHashMap)),
                new ApiCallback<BalanceBean>() {
                    @Override
                    public void onSuccess(BalanceBean model) {
                        if (model.getHttpCode() == 200) {
                            PayEvent.postNoData(PayEvent.Message.PAY_SUCCEED);
                        } else {
                            T.show(model.getMsg());
                        }

                    }

                    @Override
                    public void onNoNewWork() {

                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }

    /**
     * 优付卡支付
     * @param orderId

     * @param cpnRelId
     */
    public void confirmPay(String orderId,String  cpnRelId ) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("orderId", orderId);
        if (!CheckUtils.isEmpty(cpnRelId)){
            mHashMap.put("cpnRelId", cpnRelId);
        }
        mHashMap.put("groupCardType","41");//groupCardType 支付(团)卡类型 41-优付卡

        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));

        addSubscription(apiStores.confirmPay(Constant.createParameter(mHashMap)),
                new ApiCallback<BalanceBean>() {
                    @Override
                    public void onSuccess(BalanceBean model) {
                        if (model.getHttpCode() == 200) {
                            PayEvent.postNoData(PayEvent.Message.PAY_SUCCEED);
                        } else {
                            T.show(model.getMsg());
                        }

                    }

                    @Override
                    public void onNoNewWork() {

                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }






}
